﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using ueEngine.Framework.Common;

namespace ueEngine.Biz.Core.ExServer.Statistics
{
    public class BehaviorTrace
    {
        public void RecordUserBehavior(string clientIp, string userId, string httpMethod, string requestPath, string browser, string exchangeData, out int Id)
        {
            string conStr = ConfigurationManager.ConnectionStrings["ConnectStr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(conStr))
            {
                SqlCommand com = con.CreateCommand();
                com.CommandText = "USP_BehaviorTrace_NewRecord";
                com.Parameters.Add(new SqlParameter("@clientIp", clientIp));
                com.Parameters.Add(new SqlParameter("@userId", userId));
                com.Parameters.Add(new SqlParameter("@beginTime", DateTime.Now));
                com.Parameters.Add(new SqlParameter("@httpMethod", httpMethod));
                com.Parameters.Add(new SqlParameter("@requestPath", requestPath));
                com.Parameters.Add(new SqlParameter("@browser", browser));
                com.Parameters.Add(new SqlParameter("@exchangeData", exchangeData));
                var param = new SqlParameter("@id", System.Data.SqlDbType.Int);
                param.Direction = System.Data.ParameterDirection.Output;
                com.Parameters.Add(param);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                con.Open();
                com.ExecuteNonQuery();
                Id = com.Parameters["@Id"].Value.ToInt32(0);
                con.Close();
            }
        }
        public void UpdateCompleteTimeForBehaviorTrace(int id)
        {
            string conStr = ConfigurationManager.ConnectionStrings["ConnectStr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(conStr))
            {
                SqlCommand com = con.CreateCommand();
                com.CommandText = "update BehaviorTrace set completeTime = @completeTime where Id = @id";
                com.Parameters.Add(new SqlParameter("@completeTime", DateTime.Now));
                com.Parameters.Add(new SqlParameter("@id", id));
                com.CommandType = System.Data.CommandType.Text;
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}
